package com.example.administrator.yuejian35.Me.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.yuejian35.Config;
import com.example.administrator.yuejian35.Find.activity.MypostActivity;
import com.example.administrator.yuejian35.MainActivity;
import com.example.administrator.yuejian35.Me.MeModel;
import com.example.administrator.yuejian35.Me.activity.SettingActivity;
import com.example.administrator.yuejian35.Me.bean.UserInfo;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.Recommend.activity.ApplyListActivity;
import com.example.administrator.yuejian35.Recommend.activity.GuanzhuListActivity;
import com.example.administrator.yuejian35.RoundTransform;
import com.example.administrator.yuejian35.base.BaseFragment;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends BaseFragment {
    private ImageButton setting;
    private String Uid;
    private LinearLayout gz;
    private LinearLayout dd;
    private LinearLayout ft;
    private ImageView userpic;

    private TextView name, sex, tel, intro;

    private Context context;

    public MeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setting = (ImageButton) view.findViewById(R.id.img_setting);
        gz= (LinearLayout) view.findViewById(R.id.gz);
        dd= (LinearLayout) view.findViewById(R.id.dd);
        ft= (LinearLayout) view.findViewById(R.id.ft);
        name = (TextView) view.findViewById(R.id.me_username);
        intro = (TextView) view.findViewById(R.id.me_intro);
        sex = (TextView) view.findViewById(R.id.me_sex);
        /*citys = (TextView) findViewById(R.id.updata_city);
        submit = (TextView) findViewById(R.id.updata_submit);*/
        userpic = (ImageView) view.findViewById(R.id.me_upic);

        dd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ApplyListActivity.class);
                getActivity().startActivity(intent);
            }
        });

        ft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MypostActivity.class);
                getActivity().startActivity(intent);
            }
        });

        gz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), GuanzhuListActivity.class);
                getActivity().startActivity(intent);
            }
        });

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),SettingActivity.class);
                startActivity(intent);
            }
        });


        MainActivity mainActivity = (MainActivity) getActivity();
        if (mainActivity != null) {
            Uid = mainActivity.getUserId();
        }

        Log.v("userid", Uid);

        MeModel memodel = new MeModel();
        Call call = memodel.userinfo(Uid);

        call.enqueue(new Callback<UserInfo>() {
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                UserInfo user = response.body();
                name.setText(user.getUname());
                //tel.setText(user.getTel());
                intro.setText(user.getIntro());

                if(user.getSex().equals("1")){
                    sex.setText("男");
                }
                else {
                    sex.setText("女");
                }

                if (!user.getUserpic().equals("")) {
                    Picasso.with(context)
                            .load(Config.CoachImages_URL + user.getUserpic())
                            .transform(new RoundTransform(100))
                            .into(userpic);
                }

            }

            @Override
            public void onFailure(Call<UserInfo> call, Throwable t) {

            }

        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_me, container, false);

    }

}
